//
//  EditSelfManager.m
//  17DianYing
//
//  Created by apple on 15/9/24.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "EditSelfManager.h"
#import "AFNetworking.h"


@interface EditSelfManager()

@property(nonatomic,strong)AFHTTPRequestOperationManager *afManager;


@end



@implementation EditSelfManager

+(instancetype)sharManager
{
    static EditSelfManager *m = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        m = [[EditSelfManager alloc]init];
    });
    return m;
}


-(void)getHeadImagerWithToken:(NSString *)token andImage:(UIImage *)image andTyp:(NSString *)typ andBlock:(editSelfBlock)hb
{
//    NSString * url = @"http://192.168.191.1:8080/api/mobile/login";
//    
//    
//    
//    NSDictionary * dic =  @{@"token":token,@"file":image,@"type":typ};
//    
//    
//    
//    AFHTTPRequestOperation * op = [self.afManager POST:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        
//        id obj = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
//        NSDictionary *dic = obj;
//        hb(dic);
//
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"%@",error.localizedDescription);
//    }];
//    [op start];
//    
//    }


NSData *imageData = UIImagePNGRepresentation(image);
NSString *URL =[http stringByAppendingString:@"/mobile/message/upload"];
NSMutableURLRequest *request =[[AFHTTPRequestSerializer serializer]
                               multipartFormRequestWithMethod:@"POST"
                               URLString:URL
                               parameters:@{@"file":[NSString stringWithFormat:@"%@.png"]}
                               constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
                               {
                                   [formData appendPartWithFileData:imageData
                                                               name:@"file"
                                                           fileName:@"file"
                                                           mimeType:@"image/png"];
                               }];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    }
    else {
        NSLog(@"%@", responseObject);
        //    [UserBean setHeadUrl:savedImagePath];
        //    [self.tableView reloadData];
    }
}];
[uploadTask resume];

}

@end
